package com.kemai.designmode.ui
import android.os.Bundle
import com.kemai.common.adapter.ItemClickListener
import com.kemai.common.ui.BaseActivity
import com.kemai.designmode.R
import com.kemai.designmode.chain.ManJianDiscount
import com.kemai.designmode.chain.MultiplyDiscount
import com.kemai.designmode.chain.OtherShoppingCart
import com.kemai.designmode.chain.ZheKouDiscount
import com.kemai.designmode.entry.Fruit
import com.kemai.designmode.factory.method.AppleMethodFactory
import com.kemai.designmode.factory.method.BananaMethodFactory
import com.kemai.designmode.factory.method.OrangeMethodFactory

//责任链模式(将请求传给一个接收者链，由链将请求转给目标对象，当出现多个对象可以处理同一个请求时，适合责任链模式)
class ChainActivity : BaseActivity(), ItemClickListener {
    private val studyContent = arrayOf("开始传递")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_common_content)
        findViewById()
        setTiTle("责任链模式")
        setAdapterContent(studyContent,ChainActivity@this,ChainActivity@this)
    }

    override fun itemClick(position: Int) {
        var products = ArrayList<Fruit>()
        val appleMethodFactory = AppleMethodFactory()
        val bananaMethodFactory = BananaMethodFactory()
        val orangeMethodFactory = OrangeMethodFactory()
        val apple = appleMethodFactory.getFruit()
        val banana = bananaMethodFactory.getFruit()
        val orange = orangeMethodFactory.getFruit()
        products.add(apple)
        products.add(banana)
        products.add(orange)
        var multiplyDiscount: MultiplyDiscount = ManJianDiscount(null)
        multiplyDiscount = ZheKouDiscount(multiplyDiscount)
        when(position){
            0->{
                val otherShoppingCart = OtherShoppingCart(products,multiplyDiscount)
                otherShoppingCart.submitOrder()

            }
        }

    }

}